﻿using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wpf.Netdisk.Extensions;
using Wpf.Netdisk.Models.Entity;
using Wpf.Netdisk.Models.Event;
using Wpf.Netdisk.Services.Implement;
using Wpf.Netdisk.Services.Interface;
using Wpf.Netdisk.ViewModels.Base;

namespace Wpf.Netdisk.ViewModels.ChildrenListItem
{
    public class MyFolderViewModel : NavigationViewModel
    {
        private ObservableCollection<NetdiskFolderInfo> folders;
        private readonly INetdiskFolderService _netdiskFolderService;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _region;
        

        private NetdiskFolderInfo currentInfo;

        public NetdiskFolderInfo CurrentInfo
        {
            get { return currentInfo; }
            set { currentInfo = value; RaisePropertyChanged(); }
        }
        
        public ObservableCollection<NetdiskFolderInfo> Folders
        {
            get { return folders; }
            set { folders = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<string> GoFileViewCommand { get; set; }
        public DelegateCommand<string> ExecuteCommand { get; set; }
        //public MyFolderViewModel(INetdiskFolderService netdiskFolderService, IContainerProvider containerProvider) : base(containerProvider)
        //{
        //    UploadCommand = new DelegateCommand<OpenFileDialog>(Upload);
        //    this._netdiskFolderService = netdiskFolderService;
        //    //InitFolders();
        //}
        public MyFolderViewModel(IContainerProvider containerProvider, IEventAggregator eventAggregator, IRegionManager region) : base(containerProvider)
        {
            
            UploadCommand = new DelegateCommand<OpenFileDialog>(Upload);
            _netdiskFolderService = containerProvider.Resolve<INetdiskFolderService>();
            InitFolders();
            //ExecuteCommand = new DelegateCommand<string>(Execute);
            this._eventAggregator = eventAggregator;
            this._region = region;
            GoFileViewCommand = new DelegateCommand<string>(GoFileView);
            var myEvent = eventAggregator.GetEvent<PubSubEvent>(); 
            myEvent.Subscribe(InitFolders);
        }

        private void GoFileView(string obj)
        {
            var myEvent = _eventAggregator.GetEvent<TellMainFolderIdEvent>();
            myEvent.Publish(obj);
            //事件总线，告诉首页，当前folderid是什么
            //定义传参变量
            NavigationParameters keys = new NavigationParameters();
            //添加参数，键值对格式
            keys.Add("FolderId", obj);
            var test = _region.Regions[PrismManager.ChildrenViewRegionName];
            _region.Regions[PrismManager.ChildrenViewRegionName].RequestNavigate("FileView", Callback =>
            {

            }, keys);
        }
        private bool isRightDrawerOpen;
        public bool IsRightDrawerOpen
        {
            get { return isRightDrawerOpen; }
            set { isRightDrawerOpen = value; RaisePropertyChanged(); }
        }

        
        public async void InitFolders()
        {
            var folderRes = await _netdiskFolderService.GetListAsync(new Models.BaseModel.QueryParameter
            {
                Page = 1,
                Size = 10000
            });
            Folders = new();
            Folders.Clear();
            
            foreach (var item in folderRes.Rows)
            {
                NetdiskFolderInfo model = new();
                model.FolderName = item["FolderName"]?.ToString();
                model.Id = item["Id"].ToString();
                model.IsActive = Convert.ToInt32(item["IsActive"]);
                model.UserId = item["UserId"].ToString();
                Folders.Add(model);
            }
        }

        private void Upload(OpenFileDialog obj)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Title = "选择Exe文件";
            //openFileDialog.Filter = "exe文件|*.exe";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            openFileDialog.RestoreDirectory = true;
            //openFileDialog.DefaultExt = "exe";
            if (openFileDialog.ShowDialog() == false)
            {
                return;
            }
            else
            {

            }
            string txtFile = openFileDialog.FileName;
        }

        public DelegateCommand<OpenFileDialog> UploadCommand { get; set; }
    }
}
